package cn.pconline.autoclub.mq;

import cn.pconline.autoclub.entity.ClubLevel;
import com.alibaba.fastjson.JSONObject;
import java.io.UnsupportedEncodingException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author chenchongqi
 */
public class SimpleMqClient {

    @Autowired
    private AmqpTemplate autotaskSendMqTemplate;
    private static String defaultCharset = "UTF-8";
    private MessageProperties messageProperties = new MessageProperties();
    private static String ROUTE_KEY_MEMBER_CHECKIN = "autoclub.member.checkin";
    private static String ROUTE_KEY_Upgrade_ONE="autoclub.club.upgrade_one";//一星车友会升级KEY
    private static String ROUTE_KEY_Upgrade_TWO="autoclub.club.upgrade_two";//二星车友会升级KEY
    private static String ROUTE_KEY_Upgrade_THREE="autoclub.club.upgrade_three";//三星车友会升级KEY
    private static String ROUTE_KEY_Upgrade_FOUR="autoclub.club.upgrade_four";//四星车友会升级KEY
    private static String ROUTE_KEY_Upgrade_FIVE="autoclub.club.upgrade_five";//五星车友会升级KEY
    private static final Logger log = LoggerFactory.getLogger(SimpleMqClient.class);

    public SimpleMqClient() {
        messageProperties.setContentEncoding(defaultCharset);
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    }

    public void sendMessage(String key, JSONObject object)
            throws UnsupportedEncodingException {
        Message msg = new Message(object.toJSONString().getBytes(defaultCharset),
                messageProperties);
        try {
            autotaskSendMqTemplate.send(key, msg);
        } catch (AmqpException e) {
            log.error("Send mq message failed:" + object.toJSONString(), e);
        }
    }

    public void sendCheckinMessage(JSONObject object)
            throws UnsupportedEncodingException {
        sendMessage(ROUTE_KEY_MEMBER_CHECKIN, object);
    }
    
    /**
     * 升级发送消息
     * @param object
     * @throws UnsupportedEncodingException 
     */
    public void sendUpgradeMessage(JSONObject object){
        if( object == null ) return;
        int levelId = object.getIntValue("levelId");
        String routeKey = "";
        switch(levelId){
            case ClubLevel.LEVEL_ONE:
                routeKey = ROUTE_KEY_Upgrade_ONE;
                break;
            case ClubLevel.LEVEL_TWO:
                routeKey = ROUTE_KEY_Upgrade_TWO;
                break;
            case ClubLevel.LEVEL_THREE:
                routeKey = ROUTE_KEY_Upgrade_THREE;
                break;
            case ClubLevel.LEVEL_FOUR:
                routeKey = ROUTE_KEY_Upgrade_FOUR;
                break;
            case ClubLevel.LEVEL_FIVE:
                routeKey = ROUTE_KEY_Upgrade_FIVE;
                break;
            default:break;
        }
        if(StringUtils.isBlank(routeKey)){
            log.error("levelId routeKey not found levelId :"+levelId);
            return;
        }
        try {
            sendMessage(routeKey, object);
        } catch (UnsupportedEncodingException ex) {
            log.error("upgrade send mq error :"+ex);
        }
    }
}
